//
//  View.m
//  DrawShape
//
//  Created by apple on 15/8/5.
//  Copyright (c) 2015年 戴维营教育. All rights reserved.
//

#import "View.h"
//1. 完善初始化方法
//2. 添加方法，使得center, bounds, frame相互关联

@implementation View

- (instancetype)init {
    return [self initWithFrame:DVIRectZero];
}

- (instancetype)initWithFrame:(DVIRect)frame {
    if (self = [super init]) {
        self.frame = frame;
    }
    
    return self;
}

+ (instancetype)view {
    return [[self alloc] init];
}

+ (instancetype)viewWithFrame:(DVIRect)frame {
//    View *v = [[self alloc] init];
//    v.frame = frame;
//    return v;
    return [[self alloc] initWithFrame:frame];
}

//1. 获取一个属性（变量）发生改变的时机
//2. setter/getter
- (void)setCenter:(DVIPoint)center {
    _center = center;
    
    _frame.origin.x = _center.x - _frame.size.width / 2;
    _frame.origin.y = _center.y - _frame.size.height / 2;
}

- (void)setBounds:(DVIRect)bounds {
    _bounds = bounds;
    
    _frame.origin.x = _center.x - bounds.size.width / 2;
    _frame.origin.y = _center.y - bounds.size.height / 2;
    _frame.size = _bounds.size;
}

- (void)setFrame:(DVIRect)frame {
    _frame = frame;
    
    _center.x = _frame.origin.x + _frame.size.width / 2;
    _center.y = _frame.origin.y + _frame.size.height / 2;
    
    _bounds = DVIRectMake(0, 0, _frame.size.width, _frame.size.height);
}

- (void)dealloc {
    [super dealloc];
    
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (NSString *)description {
    //NSLog
    NSString *str = [NSString stringWithFormat:@"%p: %f, %f", self, 123.0, 34.0];
    return str;
}
@end
